﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Threading.Tasks;
using System.Windows;

namespace SpaceWindow
{
    class SpaceButton : Button
    {
        private string text;

        public string Text
        {
            get { return text; }
            set
            {
                text = value;
                this.Content = this.SpaceOutText(this.text);
            }
        }

        public int Space
        {
            get { return (int)GetValue(SpaceProperty); }
            set { SetValue(SpaceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Space.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SpaceProperty;

        static SpaceButton()
        {
            FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
            metadata.DefaultValue = 1;
            metadata.AffectsMeasure = true;
            metadata.Inherits = true;
            metadata.PropertyChangedCallback += OnSpacePropertyChanged;

            SpaceProperty =
            DependencyProperty.Register("Space", typeof(int), typeof(SpaceButton),metadata,ValidateSpaceValue);
        }

        private static bool ValidateSpaceValue(object value)
        {
            int i = (int)value;
            return i >= 0;
        }

        static void OnSpacePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SpaceButton btn = d as SpaceButton;
            btn.Content = btn.SpaceOutText(btn.text);
        }

        string SpaceOutText(string str)
        {
            if (str == null)
            {
                return null;
            }

            StringBuilder build = new StringBuilder();
            foreach (var ch in str)
            {
                build.Append(ch + new string(' ', this.Space));
            }

            return build.ToString();
        }
    }
}
